<?php
class SiteController extends ControllerBase {

    public function actionIndex() {
    	// Init object
		$criteria 	= new CDbCriteria();
		// Get slide
		$criteria->order = 'position DESC';
		$criteria->addCondition('product_id = \'0\'');
		$objSlides = Slides::model()->findAll($criteria);

		// Init object
		$criteria 	= new CDbCriteria();
		// Get category
		$criteria->order = 'position DESC, id DESC';
		$criteria->addCondition('status != \'3\'');
		$objCategories 	= Categories::model()->findAll($criteria);



		$this->render('index', compact('objSlides', 'objCategories'));
    }

    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionLanguages($lang) {
        $language = isset($_GET['lang']) ? $_GET['lang'] : Yii::app()->language;
        Yii::app()->request->cookies['language'] = new CHttpCookie('language', $lang);
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    public function actionLogout() {
        if (Yii::app()->user->isGuest) {
            $this->redirect(Yii::app()->homeUrl);
        }
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    public function actionIntro(){
        $this->render('intro');
    }

    public function actionContact(){
        $this->render('contact');
    }

    public function actionService(){
        $this->render('service');
    }

    public function actionAnouncement(){
        $this->render('anouncement');
    }

    public function actionForgotPassword() {
        if (!(Yii::app()->user->isGuest))
            $this->redirect(Yii::app()->homeUrl);

        $this->layout = '//layouts/0-1-0/1';
        Yii::import('application.modules.user.models.Recovery');
        $model = new Recovery;

        if (isset($_POST['Recovery'])) {
            $model->attributes = $_POST['Recovery'];
            if ($model->validate()) {
                // Update new password
                $newPassword = Recovery::generatePassword();
                $user = User::model()->findByPk($model->uid);
                $user->password = md5($newPassword);
                $user->update('password');

                Yii::import('ext.mail.YiiMailMessage');
                $message = new YiiMailMessage;

                // message notice change password
                $subject = TranslateUtil::t('Spring Candy - Forgot Password');
                $body = CHtml::tag('p', array(), "Hi {$user->username},");
                $body .= CHtml::tag('p', array(), "Password for {$user->username} has been reset.");
                $body .= CHtml::tag('p', array(), "Here's all the information you need to get you started .");

                $body .= CHtml::tag('p', array(), "------------------------------<br/>", false);
                $body .= 'Login Infomation';
                $body .= "------------------------------<br/>";
                $body .= "User ID: {$user->username}<br/>";
                $body .= "Password: {$newPassword}<br/>";
                $body .= CHtml::closeTag('p');

                $message->setSubject($subject);
                $message->setBody($body, 'text/html', 'utf8');

                $message->setFrom(array('admin@springcandy.com' => 'Spring Candy Webmaster'));
                $message->setReplyTo(array('admin@springcandy.com' => 'Spring Candy Webmaster'));

                // Recipient
                $recipients = array($user->email);
                $message->setTo($recipients);

                // Send mail
                if (Yii::app()->mail->send($message)) {
                    $this->setFlash(TranslateUtil::t('Password has been reset for {0}', array('{0}' => $user->username)), SiteController::FLASH_SUCCESS);
                    $this->refresh();
                }
            }
        }
        $this->render('recovery', array('model' => $model));
    }
}